package main.java.com.silence.controller;

import com.silence.constant.AttributeConstant;
import com.silence.constant.ViewNameConstant;
import com.silence.pojo.User;
import com.silence.service.IUserService;
import com.silence.utils.RandomCodeUtils;
import com.silence.vo.LoginUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Author: silence
 * Date: 17-5-26
 */
@Controller
public class UserController {

    @Resource
    private IUserService userService;


    /**
     * 处理注册
     */
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ModelAndView register(User user, ModelAndView modelAndView) {

        user.setCode(RandomCodeUtils.getUUID() + RandomCodeUtils.getUUID());
        int temp;
        temp = userService.register(user);

        if (1 == temp) {
            // 注册成功
            modelAndView.setViewName(ViewNameConstant.SHOW_MSG);
            modelAndView.addObject(AttributeConstant.MSG, "注册成功,请去邮箱激活后登录！");
        } else if (0 == temp) {
            // 已经注册
            modelAndView.setViewName(ViewNameConstant.SHOW_MSG);
            modelAndView.addObject(AttributeConstant.MSG, "该账户已经注册");
        } else {
            // 服务器问题 注册失败
            modelAndView.setViewName(ViewNameConstant.SHOW_MSG);
            modelAndView.addObject(AttributeConstant.MSG, "服务器出现问题");

        }

        return modelAndView;

    }

    /**
     * 激活账户
     */
    @RequestMapping(value = "/activate/{code}", method = RequestMethod.GET)
    public ModelAndView activateAccount(@PathVariable String code, ModelAndView modelAndView) {
        if (userService.activate(code)) {
            // 激活成功
            modelAndView.addObject(AttributeConstant.MSG, "激活成功！");
            modelAndView.setViewName(ViewNameConstant.LOGIN);
            return modelAndView;
        } else {
            // 激活失败
            modelAndView.addObject(AttributeConstant.MSG, "激活码错误！");
            modelAndView.setViewName("showMsg");
            return modelAndView;
        }

    }

    /**
     * 处理登录
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(LoginUser loginUser, HttpServletRequest request, ModelAndView modelAndView) {

        int temp = userService.login(loginUser);

        if ( temp == 1) {   // 登录成功

            // 登录成功后 向session中存值或者向cookie中存值
            User user = userService.findUserByEmail(loginUser.getEmail());

            request.getSession().setAttribute(AttributeConstant.LOGIN_USER_INFO, user);
            request.getSession().setAttribute(AttributeConstant.IS_LOGIN, 1);

            // 设置跳转的页面 HOME.jsp
            modelAndView.setViewName(ViewNameConstant.REDIRECT_HOME);

            // 将该用户的信息、账单等添加到model 即显示到home页面

        } else if (temp == -1){     // 密码错误

            // 登录失败 将登录失败的信息显示到该页面
            modelAndView.setViewName(ViewNameConstant.LOGIN);

            modelAndView.addObject(AttributeConstant.MSG, "登录失败，请检查邮箱或密码是否正确");

        } else {    // 不存在该用户

            modelAndView.setViewName(ViewNameConstant.LOGIN);
            modelAndView.addObject(AttributeConstant.MSG, "该账户不存在，请注册");
        }
        return modelAndView;

    }

}
